package com.cfh.service;

import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;

import com.cfh.ahead.Constant;
import com.cfh.domain.Config;
import com.cfh.domain.ConfigExample;
import com.cfh.domain.Product;
import com.cfh.domain.ProductExample;
import com.cfh.persistence.dao.ConfigDAO;
import com.cfh.persistence.dao.ProductDAO;

public class ProductServiceImpl implements ProductService  {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ProductServiceImpl.class);

	/**
	 * Logger for this class
	 */

	private ProductDAO ProductDAO;
	private ConfigDAO configDAO;

	

	public void insert(Product Product) {
		ProductDAO.insert(Product);
	}

	public List selectByExample(ProductExample example, int start, int limit) {
		List list = ProductDAO.selectByExample(example, start, limit);
		return AddMMTypeDisc(list);
	}
	
	private List AddMMTypeDisc(List list) {
		if (list == null) {
			return null;
		}
		ConfigExample example = new ConfigExample();
		example.createCriteria().andCFG_TYPEEqualTo(Constant.CFG_TYPE_PRDTYPE);
		List typeList = configDAO.selectByExample(example);
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Product prd = (Product) iterator.next();
			for (Iterator iterator2 = typeList.iterator(); iterator2.hasNext();) {
				Config type = (Config) iterator2.next();
				if (prd != null && prd.getPRD_TYPE() != null && prd.getPRD_TYPE().equals(type.getCFG_CODE())) {
					prd.setMM_TYPE_DESC(type.getCFG_VALUE());
				}
			}
		}

		return list;
	}

	public int countByExample(ProductExample example) {
		return ProductDAO.countByExample(example);
	}

	public int update(Product Product) {
		return ProductDAO.updateByPrimaryKeySelective(Product);
	}

	public int deleteByExample(ProductExample example) {
		return ProductDAO.deleteByExample(example);
	}

	public void setProductDAO(ProductDAO ProductDAO) {
		this.ProductDAO = ProductDAO;
	}

	public ProductDAO getProductDAO() {
		return ProductDAO;
	}

	//TODO REMOVE IT
	public Long getCurrentStage() {
		return new Long(0);
		
	}

	public int deleteByPrimaryKey(String key) {
		return ProductDAO.deleteByPrimaryKey(key);
	}

	public void setConfigDAO(ConfigDAO configDAO) {
		this.configDAO = configDAO;
	}

	public ConfigDAO getConfigDAO() {
		return configDAO;
	}

}
